package cn.kgc.mybatis.utils;

import com.mysql.jdbc.Driver;

import java.sql.*;

/**
 * @author: mosin
 * @version: v1.0  2022/9/26
 * jdbc操作工具类
 * 1.获取连接
 * 2.释放资源
 */
public class JDBCUtil {


    public static Connection getConnection() throws SQLException {

        //注册驱动
        Driver driver = new Driver();
        DriverManager.registerDriver(driver);
        // 获取连接
        String url = "jdbc:mysql:///java2216?useSSL=false";
        String username = "root";
        String password = "root";
        Connection connection = DriverManager.getConnection(url, username, password);

        return connection;
    }


    public static void closeAll(ResultSet resultSet, Statement statement,Connection connection){


        try {
            if(resultSet!=null){
                resultSet.close();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


        try {
            if(statement!=null){
                statement.close();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        try {
            if(connection!=null){
                connection.close();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }


}
